const fs = require("fs");
const path = require("path");
const rollup = require("rollup");
let builds = require("./config").getAllBuilds();

build(builds);

function build(builds) {
  let built = 0;
  const total = builds.length;
  const next = () => {
    buildEntry(builds[built])
      .then(() => {
        built++;
        if (built < total) {
          next();
        }
      })
      .catch(logError);
  };
  next();
}

function buildEntry(config) {
  const output = config.output;
  const { file, banner } = output;
  return rollup
    .rollup(config)
    .then(bundle => bundle.generate(output))
    .then(({ output: [{ code }] }) => {
      return write(file, code);
    });
}

function write(dest, code) {
  return new Promise((resolve, reject) => {
    function report(extra) {
      console.log(
        blue(path.relative(process.cwd(), dest)) +
          " " +
          getSize(code) +
          (extra || "")
      );
      resolve();
    }

    fs.writeFile(dest, code, err => {
      if (err) return reject(err);
      report();
    });
  });
}

function logError(e) {
  console.log(e);
}

function blue(str) {
  return "\x1b[1m\x1b[34m" + str + "\x1b[39m\x1b[22m";
}

function getSize(code) {
  return (code.length / 1024).toFixed(2) + "kb";
}
